﻿using System;
using System.ComponentModel;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Win.Templates.ActionContainers;
using DevExpress.XtraBars;
using DevExpress.ExpressApp;
using System.Windows.Forms;
using Hdt.TransOcean.Dal;
using System.IO;

namespace Hdt.TransOcean.Presentation
{
    public partial class FormEmployee : DevExpress.ExpressApp.Win.Templates.XtraFormTemplateBase, ISupportClassicToRibbonTransform
    {
        private EOperation _operation;
        //private Employee _employee;


        //public static EOperation Operation
        //{
        //    get { return _operation; }
        //    set { _operation = value; }
        //}


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            CheckTransformToRibbon();
            hdtManageControl1.Width = this.Width;
        }
        protected override IModelFormState GetFormStateNode()
        {
            if (View != null)
            {
                return TemplatesHelper.GetFormStateNode(View.Id);
            }
            else
            {
                return base.GetFormStateNode();
            }
        }
        public override void SetSettings(IModelTemplate modelTemplate)
        {
            base.SetSettings(modelTemplate);
            formStateModelSynchronizerComponent.Model = GetFormStateNode();
        }
        [Obsolete("Use the DetailViewForm() constructor"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public FormEmployee(XafApplication application) : this() { }
        public FormEmployee()
        {
            InitializeComponent();
        }

        public FormEmployee(EOperation operation, string code = null)
        {
            InitializeComponent();
            this._operation = operation;
            txtCode.Enabled = false;
            Presentation.BindGridLookupEdit(gridLookUpEditDepartment, Presentation.GetData(EObject.Department));
            Presentation.BindGridLookupEdit(gridLookUpEditTitle, Presentation.GetData(EObject.Title));
            Presentation.DisplayButtonByOperation(hdtManageControl1,operation);
            if (operation == EOperation.New)
            {
                txtCode.Text = Dao.GetAutoCode("Employee");
                chkUsed.Enabled = false;
            }
            if (operation == EOperation.Edit)
            {
                if (!String.IsNullOrEmpty(code))
                {
                    Employee employee = Dao.SelectEmployee(code)[0];

                    txtCode.Text = employee.code;
                    txtName.Text = employee.name;
                    txtEmail.Text = employee.email;
                    txtDescription.Text = employee.description;                    
                    txtTelephone.Text = employee.phone;
                    gridLookUpEditDepartment.EditValue = employee.department_code;
                    gridLookUpEditTitle.EditValue = employee.title_code;
                    chkUsed.EditValue = employee.status;
                }
            }
        }

        

        private void hdtManageControl1_HdtSaveButtonClick()
        {
            string message = "Lưu thành công!";
            //bool flag = true;
            Dao dao = new Dao();
            Employee employee = new Employee();
            employee.code = txtCode.Text;
            employee.name = txtName.Text;
            employee.phone = txtTelephone.Text;
            employee.email = txtEmail.Text;
            employee.description = txtDescription.Text;
            employee.title_code = (int)gridLookUpEditTitle.EditValue;
            employee.department_code = (int)gridLookUpEditDepartment.EditValue ;
            employee.user_code = String.Empty;
            employee.status = 0;        

            try
            {
                if (Presentation.ValidEmployee(employee) == true)
                {
                    if(this._operation == EOperation.Edit)
                    {
                        Dao.UpdateEmployee(employee);
                    }
                    else if(this._operation == EOperation.New)
                    {
                        if (Dao.InsertEmployee(employee))
                        {
                            //Presentation.ViewControl(this, EOperation.New);
                            AutoCode ac = new AutoCode();
                            ac.current_value = txtCode.Text.ToString().Substring(2,txtCode.Text.ToString().Length-2);
                            Dao.UpdateAutoCodeByTable(ac, "Employee");
                            ResetEmployeeInfo();
                        }
                    }
                }
                
            }
            catch (Exception e)
            {
                if (e != null)
                {
             //       flag = false;
                    message = e.ToString();
                }
            }
            finally
            {
                MessageBox.Show(message, "Nhân viên - Lưu");
            }
        }

        private void hdtManageControl1_HdtExportButtonClick()
        {
            string [] titles = new string[] {"Mã", "Tên", "Địa chỉ", "Điện thoại", "Di động", "Fax", "Email", "Số ngày nợ", "Mã số thuế", "Ghi chú"};

            Dao.ExportExcel(EObject.Employee, "employee.xls", null);
            
        }

        private void hdtManageControl1_HdtImportButtonClick()
        {
            try
            {
                Dao.ImportExcel(EObject.Employee);
            }
            catch
            {
                throw;
            }
        }

        private void hdtManageControl1_HdtNewButtonClick()
        {
            txtCode.Enabled = false;
            ResetEmployeeInfo();
            txtCode.Text = Dao.GetAutoCode("Employee");
            
            Presentation.DisplayButtonByOperation(hdtManageControl1, EOperation.New);
        }

        private void ResetEmployeeInfo()
        {
            txtCode.Text = String.Empty;
            txtDescription.Text = String.Empty;
            txtEmail.Text = String.Empty;
            txtName.Text = String.Empty;
            txtTelephone.Text = String.Empty;
            gridLookUpEditTitle.EditValue = 1;
            gridLookUpEditTitle.EditValue = 1;
            chkUsed.EditValue = 0;
        }

        private void hdtManageControl1_HdtEditButtonClick()
        {
            Presentation.DisplayButtonByOperation(hdtManageControl1, EOperation.Edit);
        }

        private void hdtManageControl1_HdtDeleteButtonClick()
        {
            if (!String.IsNullOrEmpty(txtCode.Text))
            {
                try
                {
                    if (MessageBox.Show("Bạn muốn xóa không?", "Nhân viên", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
                    {
                        if (Dao.DeleteEmployeeByCode(txtCode.Text) == true)
                        {
                            MessageBox.Show("Xóa thành công", "Nhân viên");
                            Presentation.DisplayButtonByOperation(hdtManageControl1, EOperation.Delete);
                        }
                    }
                }
                catch (Exception e)
                {

                    MessageBox.Show(e.ToString());
                }
            }
           
        }

        private void hdtManageControl1_HdtCloseButtonClick()
        {
            this.Close();
        }

        private void FormEmployee_Load(object sender, EventArgs e)
        {
            

        }

    }
}
